<template>
  <a-modal
    :visible="visible"
    :title="title"
    :width="900"
    @cancel="() => { $emit('cancel') }"
    @ok="comfirm"
  >
    <!-- <detail-list :col="4">
      <detail-list-item term="培养单位"></detail-list-item>
      <detail-list-item term="课程名称"></detail-list-item>
      <detail-list-item term="上课周类型"></detail-list-item>
      <detail-list-item term="上课周"></detail-list-item>
      <detail-list-item term="开始周"></detail-list-item>
      <detail-list-item term="结束周"></detail-list-item>
      <detail-list-item term="开始课节"></detail-list-item>
      <detail-list-item term="结束课节"></detail-list-item>
      <detail-list-item term="开课学年"></detail-list-item>
      <detail-list-item term="开课学期"></detail-list-item>
      <detail-list-item v-if="type === 'classroom'" term="听课人数"></detail-list-item>
    </detail-list> -->
    <a-row style="color: #000">
      <template v-if="type === 'teacher'">
        <a-col :md="8" :sm="12" style="margin-bottom:10px;">
          <a-col :span="8">院系：</a-col>
          <a-col :span="15">
            <a-select style="width:100%;" allowClear v-model="searchForm.dwh" showSearch :filterOption="filterOption">
            <!-- <a-select style="width:100%;" :disabled="authYxList.length === 1" allowClear v-model="searchForm.dwh" showSearch :filterOption="filterOption"> -->
              <a-select-option v-for="i in yxList" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
            </a-select>
          </a-col>
        </a-col>
        <a-col :md="8" :sm="12" style="margin-bottom:10px;">
          <a-col :span="8">教师工号：</a-col>
          <a-col :span="15">
            <a-input v-model="searchForm.gh"/>
          </a-col>
        </a-col>
        <a-col :md="8" :sm="12" style="margin-bottom:10px;">
          <a-col :span="8">教师姓名：</a-col>
          <a-col :span="15">
            <a-input v-model="searchForm.xm"/>
          </a-col>
        </a-col>
      </template>

      <template v-if="type === 'classroom'">
        <a-col :md="8" :sm="12" style="margin-bottom:10px;">
          <a-col :span="8">校区：</a-col>
          <a-col :span="15">
            <a-select style="width:100%" allowClear v-model="searchForm.jsxq">
              <a-select-option v-for="i in selectMenu.campusList" :key="i.id" :value="i.xqbh">{{ i.xqmc }}</a-select-option>
            </a-select>
          </a-col>
        </a-col>
        <a-col :md="8" :sm="12" style="margin-bottom:10px;">
          <a-col :span="8">教室类型：</a-col>
          <a-col :span="15">
            <a-select style="width:100%" allowClear v-model="searchForm.typeCode">
              <a-select-option v-for="i in TYPE0046" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
            </a-select>
          </a-col>
        </a-col>
        <a-col :md="8" :sm="12" style="margin-bottom:10px;">
          <a-col :span="8">所属院系：</a-col>
          <a-col :span="15">
            <a-select style="width:100%;" allowClear v-model="searchForm.deptCode" showSearch :filterOption="filterOption">
            <!-- <a-select style="width:100%;" :disabled="authYxList && authYxList.length === 1" allowClear v-model="searchForm.deptCode" showSearch :filterOption="filterOption"> -->
              <a-select-option v-for="i in yxList" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
            </a-select>
          </a-col>
        </a-col>
        <a-col :md="8" :sm="12" style="margin-bottom:10px;">
          <a-col :span="8">教室名称：</a-col>
          <a-col :span="15">
            <a-input v-model="searchForm.classroomNo"/>
          </a-col>
        </a-col>
      </template>
      <!-- <template v-if="type === 'student'">
        <a-col :md="8" :sm="12" style="margin-bottom:10px;">
          <a-col :span="8">院系：</a-col>
          <a-col :span="15">
            <a-select style="width:100%;" allowClear @change="yxChange" v-model="searchForm.ssyxm" showSearch :filterOption="filterOption">
              <a-select-option v-for="i in yxList" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
            </a-select>
          </a-col>
        </a-col>
         <a-col :md="8" :sm="12" style="margin-bottom:10px;">
          <a-col :span="8">专业：</a-col>
          <a-col :span="15">
            <a-select style="width:100%;" allowClear v-model="searchForm.sszym" showSearch :filterOption="filterOption">
              <a-select-option v-for="(i,index) in selectMenu.zyList" :key="index" :value="i.zyh">{{ i.zyh}} {{ i.zymc }}</a-select-option>
            </a-select>
          </a-col>
        </a-col>
        <a-col :md="8" :sm="12" style="margin-bottom:10px;">
          <a-col :span="8">年级：</a-col>
          <a-col :span="15">
            <a-select style="width:100%;" allowClear v-model="searchForm.sznj">
              <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
            </a-select>
          </a-col>
        </a-col>
        <a-col :md="8" :sm="12" style="margin-bottom:10px;">
          <a-col :span="8">学号：</a-col>
          <a-col :span="15">
            <a-input v-model="searchForm.xh"/>
          </a-col>
        </a-col>
        <a-col :md="8" :sm="12" style="margin-bottom:10px;">
          <a-col :span="8">姓名：</a-col>
          <a-col :span="15">
            <a-input v-model="searchForm.xm"/>
          </a-col>
        </a-col>
      </template> -->

      <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
    </a-row>
    <s-table
      ref="table"
      size="default"
      rowKey="id"
      :columns="columns"
      :data="loadData"
      :alert="options.alert"
      :rowSelection="options.rowSelection"
      :pagination="{ pageSize: 10 }"
    >
      <div
        slot="expandedRowRender"
        slot-scope="record"
        style="margin: 0">
        <div v-for="pk in record.pkxxs" :key="pk.id" style="margin-bottom: 5px;">
          {{ pk.kcbm }}&nbsp;
          {{ pk.kcmc }}&nbsp;
          {{ pk.kclbmc }}&nbsp;
          {{ pk.skzlxmc }}&nbsp;
          开始周：{{ pk.ksz }}&nbsp;
          结束周：{{ pk.jsz }}&nbsp;
          {{ pk.skzmc }}&nbsp;
          开始课节：{{ pk.kskj }}&nbsp;
          结束课节：{{ pk.jskj }}&nbsp;
          {{ type === 'classroom' ? '' : pk.classroomNo }}
        </div>
      </div>
    </s-table>

    <!--<a-textarea v-if="type === 'teacher'" v-model="teacherNote" placeholder="任课教师说明（500字以内）" :maxlength="500" :rows="4"/>-->
  </a-modal>

</template>

<script>
import { mapState } from 'vuex'
import { STable } from '~'
import DetailList from '~/tool/DetailList'
const DetailListItem = DetailList.Item

export default {
  props: ['title', 'visible', 'type', 'TYPE0126', 'editId', 'authYxList'],
  components: {DetailList, DetailListItem, STable},
  data () {
    return {
      yxList:[],
      searchForm: {},
      selectMenu: {},
      columns: [],
      loadData: parameter => {
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.searchForm, parameter)
        // delete params.classroomNo
        let api = this.type === 'teacher' ? 'pkTeacherList' : (this.type === 'classroom' ? 'pkClassroomList' : 'pkStudentList')
        return this.$api.cultivate[api](params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        }).catch(() => {
          return {
            data: [],
            pageNo: 1,
            pageSize: 10,
            totalPage: 1,
            totalCount: 0
          }
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      teacherNote: ''
    }
  },
  watch: {
    type (val, oldVal) {
      if (val === 'teacher') {
        this.columns = [
          {
            title: '工号',
            dataIndex: 'gh'
          },
          {
            title: '姓名',
            dataIndex: 'xm'
          },
          {
            title: '所属院系',
            dataIndex: 'ssyxmc'
          },
          {
            title: '教师职称',
            dataIndex: 'zcmc'
          }
        ]
      } else if (val === 'classroom') {
        this.columns = [
          {
            title: '校区',
            dataIndex: 'jsxqmc'
          },
          {
            title: '教室类型',
            dataIndex: 'typeCodeName'
          },
          {
            title: '所属院系',
            dataIndex: 'deptName'
          },
          {
            title: '教室名称',
            dataIndex: 'classroomNo'
          },
          {
            title: '位置',
            dataIndex: 'location'
          },
          {
            title: '容量',
            dataIndex: 'capacity'
          }
        ]
      } else if (val === 'student') {
        this.columns = [
          {
            title: '学号',
            dataIndex: 'xh'
          },
          {
            title: '姓名',
            dataIndex: 'xm'
          },
          {
            title: '培养单位',
            dataIndex: 'dwmc'
          },
          {
            title: '所属专业',
            dataIndex: 'sszymc'
          },
          {
            title: '是否选择培养计划课程',
            dataIndex: 'capacity'
          },
          {
            title: '修课状态',
            dataIndex: 'xkztmc'
          }
        ]
      }

      if (oldVal !== null) {
        this.$nextTick(() => {
          this.$refs.table.refresh(true)
          this.$refs.table.clearSelected()
        })
      }
      this.$nextTick(() => {
        this.$refs.table.clearSelected()
        this.selectedRowKeys = []
        this.selectedRows = []
      })
    }
  },
  computed: {
    ...mapState({
      TYPE0046: state => state.app['TYPE0046']
    })
  },
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0046')
    this.$api.base.campusAll().then(res => {
      this.selectMenu.campusList = res.data
    })
    this.$api.base.collegeNoAuth().then(res => {
        this.yxList = res.data
    })
    if (this.authYxList && this.authYxList.length === 1) {
      this.$set(this.searchForm, 'deptCode', this.authYxList[0].dwh)
      // this.$set(this.searchForm, 'dwh', this.authYxList[0].dwh)
    }
  },
  methods: {
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    yxChange (code) {
      this.$set(this.searchForm, 'sszym', '')
      if (code) {
        this.$api.base.majorAll({lsdwh: code}).then(res => {
          this.$set(this.selectMenu, 'zyList', res.data)
        })
      } else {
        this.$set(this.selectMenu, 'zyList', [])
      }
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    comfirm () {
      if (!this.selectedRowKeys.length) {
        return this.$emit('cancel')
      } else if (this.selectedRowKeys.length > 1 && this.type !== 'student') {
        return this.$message.warning(`只能选择一个${this.type === 'teacher' ? '教师' : '教室'}`)
      }
      let apiFn = this.type === 'teacher' ? 'pkTeacherUpdate' : (this.type === 'classroom' ? 'pkClassroomUpdate' : 'pkStudentAdd')
      let params = {
        id: this.editId,
        classroomNo: this.selectedRows[0].classroomNo,
        gh: this.selectedRows[0].gh
      }
      let obj = {}
      if (this.type === 'teacher') {
        obj = {
          jsssyx: this.selectedRows[0].ssyxmc,
          rkjsmc: this.selectedRows[0].xm,
          rkjszgh: this.selectedRows[0].gh,
          jszcmc: this.selectedRows[0].zcmc
        }
      } else if (this.type === 'classroom') {
        obj = {
          xqmc: this.selectedRows[0].jsxqmc,
          jslxmc: this.selectedRows[0].typeCodeName,
          classroomNo: this.selectedRows[0].classroomNo,
          capacity: this.selectedRows[0].capacity
        }
      }
      this.$emit('updatePk', obj)
      this.$emit('cancel')
      // if (this.type === 'teacher' && !this.teacherNote) {
      //   return this.$message.warn('请输入任课教师说明')
      // }
/*      this.$api.cultivate[apiFn](params).then(res => {
        this.$message.success(res.msg)
        let obj = {}
        if (this.type === 'teacher') {
          obj = {
            jsssyx: this.selectedRows[0].ssyxmc,
            jsmc: this.selectedRows[0].xm,
            rkjszgh: this.selectedRows[0].gh,
            jszcmc: this.selectedRows[0].zcmc,
            rkjssm: this.teacherNote
          }
        } else if (this.type === 'classroom') {
          obj = {
            xqmc: this.selectedRows[0].jsxqmc,
            jslxmc: this.selectedRows[0].typeCodeName,
            classroomNo: this.selectedRows[0].classroomNo
          }
        }
        this.$emit('updatePk', obj)
        this.$emit('cancel')
      })*/
    }
  }
}
</script>

<style>

</style>
